{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 梯度下降算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Voyager\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:14: RuntimeWarning: overflow encountered in exp\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "from numpy import * \n",
    "\n",
    "def loadDataSet():\n",
    "    dataMat = []; labelMat = []\n",
    "    fr = open('testSet.txt')\n",
    "    for line in fr.readlines():\n",
    "        lineArr = line.strip().split()\n",
    "        dataMat.append([1, float(lineArr[0]), float(lineArr[1])])\n",
    "        labelMat.append(int(lineArr[2]))\n",
    "    return dataMat, labelMat\n",
    "\n",
    "\n",
    "def sigmoid(inX):\n",
    "    return 1.0 / (1+exp(-inX))\n",
    "\n",
    "\n",
    "def gradAscent(dataMatIn, classLabels):\n",
    "    dataMatrix = mat(dataMatIn)\n",
    "    labelMat = mat(classLabels).transpose()\n",
    "    m, n = shape(dataMatrix)\n",
    "    alpha = 0.001\n",
    "    maxCycle = 500\n",
    "    weights = ones((n, 1))\n",
    "    for k in range(maxCycle):\n",
    "        h = sigmoid(dataMatrix * weights)\n",
    "        error = (labelMat - h)\n",
    "        weights = weights + alpha + dataMatrix.transpose() * error\n",
    "    return weights \n",
    "\n",
    "\n",
    "dataArr, labelArr = loadDataSet()\n",
    "weights = gradAscent(dataArr, labelArr)\n",
    "weights = weights.flatten().A[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEKCAYAAAAFJbKyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpY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EwN17ga8Bn80d+iyw3t2fBz4PvCuq2ESmSolDJDxfBl5jZh8E/gL4IoC7/xQ4EmVgIlOhtapEQuLuQ2b2P4EfAVfmlkUXSTy1OETCdRWwG0jEZkYi1VDiEAmJmV1Mdue+1wD/LbfDnEjiKXGIhCC30urXyO7p8ALZgvgXoo1KpDaUOETCcSPwgrs/lLv/VeA8M3u9mf0C+H/AFWbWZ2axXZlXpBStjisiIoGoxSEiIoEocYiISCBKHCIiEogSh4iIBKLEISIigShxiIhIIEocIiISiBKHiIgE8v8BCuzhjsjeYBYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画出决策边界\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def plotBestFit(weights):\n",
    "    dataMat, labelMat = loadDataSet()\n",
    "    dataArr = array(dataMat)\n",
    "    n = shape(dataArr)[0]\n",
    "    xcord1, ycord1 = [], []\n",
    "    xcord2, ycord2 = [], []\n",
    "    for i in range(n):\n",
    "        if int(labelMat[i]) == 1:\n",
    "            xcord1.append(dataArr[i, 1]); ycord1.append(dataArr[i, 2])\n",
    "        else:\n",
    "            xcord2.append(dataArr[i, 1]); ycord2.append(dataArr[i, 2])\n",
    "    fig = plt.figure()\n",
    "    ax = fig.add_subplot(111)\n",
    "    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')\n",
    "    ax.scatter(xcord2, ycord2, s=30, c='green')\n",
    "    x = arange(-3, 3, 0.1)\n",
    "    y = (-weights[0] - weights[1] * x) / weights[2]\n",
    "    ax.plot(x, y)\n",
    "    plt.xlabel('X1'); plt.ylabel('X2')\n",
    "    plt.show()\n",
    "    \n",
    "\n",
    "plotBestFit(weights)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 随机梯度下降算法\n",
    "\n",
    "一次仅用一个样本更新回归系数。\n",
    "\n",
    "是在线学习算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weights: [ 2.38604812  0.95218922 -0.72978254] (3,)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEKCAYAAAAFJbKyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xt81PWd7/HXJwk3gYDcEi5BUCKQeK2RYPGCIgFtt3b3sRftsXXbbtE+Wlt3t4fettuedvecLt3uble3VVw9tVvbntPTrfVhvQSxrtYKEhBEAgriJdySABJArkk+54+ZpCGZTGaSmfn9fpP38/HIg8xvfjP5TCb8PvP9fr4Xc3dERERSVRB0ACIiEi1KHCIikhYlDhERSYsSh4iIpEWJQ0RE0qLEISIiaVHiEBGRtChxiIhIWpQ4REQkLUVBB5ANEyZM8BkzZgQdhohIZKxfv36/u09M5dy8TBwzZsygrq4u6DBERCLDzN5O9Vx1VYmISFqUOEREJC1KHCIikpasJw4ze9DMmszs1S7HvmFmu81sY/zrxl4eu9TMXjOzHWb2pWzHKiIifctFi+OHwNIEx//Z3S+Jfz3e/U4zKwT+DbgBqABuMbOKrEYqIiJ9ynricPfngIP9eOg8YIe773T3U8DPgJsyGpyIiKQtyBrHZ83slXhX1tkJ7p8KNHS5vSt+TEREAhRU4vgBcB5wCbAX+G6CcyzBsV73uTWzZWZWZ2Z1zc3NmYlSJMIaWhq48/E7mXf/PO58/E4aWhr6fpBICgKZAOjujR3fm9n9wGMJTtsFlHW5PQ3Yk+Q5VwIrAaqqqrSRugxqDS0NXHzvxRw9dZTT7afZuG8jD29+mE13bKJsTFnfTyCSRCAtDjOb3OXmHwKvJjhtHVBuZjPNbChwM/BoLuITiboVL6zoTBoAp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adata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def stocGradAscent0(dataMatrix, classLabels):\n",
    "    m, n = shape(dataMatrix)\n",
    "    alpha = 0.1 \n",
    "    weights = ones(n)\n",
    "    for i in range(m):\n",
    "        h = sigmoid(sum(dataMatrix[i] * weights))\n",
    "        error = classLabels[i] - h\n",
    "        weights = weights + alpha * error * array(dataMatrix[i])\n",
    "    return weights \n",
    "\n",
    "\n",
    "weights = stocGradAscent0(dataArr, labelArr)\n",
    "print('weights:', weights, weights.shape)\n",
    "plotBestFit(weights)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 改进的随机梯度下降算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def stocGradAscent1(dataMatrix, classLabels, numIter=150):\n",
    "    m, n = shape(dataMatrix)\n",
    "    weights = ones(n)\n",
    "    for j in range(numIter):\n",
    "        dataIndex = range(m)\n",
    "        for i in range(m):\n",
    "            alpha = 4 / (1.0+j+i) + 0.01                           # 每次迭代调正 alpha\n",
    "            randIndex = int(random.uniform(0, len(dataIndex)))      # 随机选取样本\n",
    "            h = sigmoid(sum(dataMatrix[randIndex]*weights))\n",
    "            error = classLabels[randIndex] - h\n",
    "            weights = weights + alpha * error * dataMatrix[randIndex]\n",
    "            del (dataIndex[randIndex])\n",
    "    return weights \n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
